package com.chaodev.base.utils;


import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class ParamDecrypt {
    public static final String KEY = "bW5vcHFyc3R1dnd4";
    public static final String IV = "7441C64260FC5485";

    public Object run(Object[] args) {
        //String key="bW5vcHFyc3R1dnd4";
        //String iv="7441C64260FC5485";
        StringBuilder result = new StringBuilder();
        Object para;
        for (Object arg : args) {
            para = arg;
            String decpara = "";
            try {
                decpara = decryptAES(para.toString(), KEY, IV);
            } catch (Exception e) {
                e.printStackTrace();
                decpara = "[decerror]";
            }
            result.append(decpara);
        }
        return result.toString();
    }

    public static String EncodeAES(String data) {
        try {
            String cipherText = encryptAES(data, KEY, IV);
            byte[] dataBytes = cipherText.getBytes(StandardCharsets.UTF_8);
            return Base64Encode(dataBytes).trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param data 明文
     * @param key  密钥，长度16
     * @param iv   偏移量，长度16
     * @return 密文
     * @author miracle.qu
     */
    public static String encryptAES(String data, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
            int plaintextLength = dataBytes.length;

            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return Base64Encode(encrypted).trim();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param data 密文
     * @param key  密钥，长度16
     * @param iv   偏移量，长度16
     * @return 明文
     * @author miracle.qu
     */
    public static String decryptAES(String data, String key, String iv) {
        try {
            byte[] encrypted1 = Base64Decode(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, StandardCharsets.UTF_8);
            return originalString.trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Base64编码
     *
     * @param byteArray
     * @return
     */
    public static String Base64Encode(byte[] byteArray) {
        return new String(Base64Utils.encode(byteArray));
    }

    /**
     * Base64解码
     *
     * @param base64EncodedString
     * @return
     */
    public static byte[] Base64Decode(String base64EncodedString) {
        return Base64Utils.decode(base64EncodedString.getBytes(StandardCharsets.UTF_8));
    }
}